#coding=utf-8

import traceback
from dbs.models import *
from common.utils import *
from common.write_log import writelog


class ControllerBase(object):
    # 常量映射
    word_map = {}

    def __init__(self):
        self.db = db

    # 把数据库对象，转成普通dict
    @classmethod
    def serialize(cls, model):
        return model_serialize(model)

    # 做outerjoin查询，可用这个函数判断判断下是否重复，然后整合返回列表
    @classmethod
    def get_item_in_list(cls, dict_list, key, value):
        return get_item_in_list(dict_list, key, value)

    # 把dict对象属性，循环赋值给数据库对象
    @classmethod
    def copy_property(cls, db_item, src_dict):
        for key in src_dict:
            db_item.__setattr__(key, src_dict[key])

    # 根据常量id，拿它的名称
    @classmethod
    def get_word_name(cls, item_id):
        item_id = int(item_id)
        word_name = cls.word_map.get(item_id)
        if not word_name:
            word_item = db.session.query(WordItem).filter(WordItem.item_id == item_id).first()
            if word_item:
                word_name = word_item.item_name
                cls.word_map[item_id] = word_name

        return word_name
